<?php

require_once "../util.php";
require_once "../config.php";

connect_db_mute();

$tag = mysql_real_escape_string($_GET['tag']);
$minimum_followers_amount = $_GET['minimum_followers_amount'];
$maximum_followers_amount = $_GET['maximum_followers_amount'];

if (empty($minimum_followers_amount))
    $minimum_followers_amount = 0;

if (empty($maximum_followers_amount))
    $maximum_followers_amount = 50000;

$sql = sprintf("select id from local_user where tags like '%%%s%%'", $tag);
$ret = sql_or_die($sql);

$candidates = array();

$today = date('Y-m-d');

while ($each_user = mysql_fetch_assoc($ret)) {
    $user_id = $each_user["id"];
    $sql = sprintf("select * from user_statistic where user_id = '%s' and collect_date like '%s' and followed_count between %s and %s", $user_id, $today, $minimum_followers_amount, $maximum_followers_amount);

    $cur_ret = sql_or_die($sql);
    $cur = array();

    while ($record = mysql_fetch_assoc($cur_ret)) {
        if (count($cur) == 0 || $cur['follow_count'] < $record['follow_count']) {
            $cur = $record;
        }
  }

    if (count($cur) > 0)
        $candidates[] = $cur;
}

echo json_encode($candidates);
